package com.gmu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gmu.domain.ResResult;
import com.gmu.mapper.TblActivitiesAddressMapper;
import com.gmu.pojo.Page;
import com.gmu.pojo.TblActivitiesAddress;
import com.gmu.service.TblActivitiesAddressService;
import com.gmu.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动地点记录 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Api(value = "窗口号管理", tags = "窗口号管理API")
@RestController
@RequestMapping("/gmu/tbl-activities-address")
public class TblActivitiesAddressController {

    @Autowired
    private TblActivitiesAddressService tblActivitiesAddressService;

    @Autowired
    private TblActivitiesAddressMapper tblActivitiesAddressMapper;

    /**
     * 采样地点设置窗口数量
     * @param activitiesId 活动id
     * @param addressId 采样地点id
     * @param addressNumber 窗口数量
     * @return
     */
    @PreAuthorize("hasAuthority('sys:windowsmanager')")
    @ApiOperation(value = "设置窗口数量")
    @PostMapping("/setAddressNumber/{activitiesId}/{addressId}/{addressNumber}")
    public ResResult<?> setAddressNumber(@PathVariable Integer activitiesId,
                                         @PathVariable Integer addressId,
                                         @PathVariable Integer addressNumber){
        return tblActivitiesAddressService.setAddressNumber(activitiesId,addressId,addressNumber);
    }

    /**
     * 选择窗口号
     */
    @ApiOperation(value = "选择窗口号")
    @PostMapping("selectWindows/{activitiesId}/{addressId}")
    public ResResult<?> selectWindows(@PathVariable Integer activitiesId,@PathVariable Integer addressId){
        return tblActivitiesAddressService.selectWindows(activitiesId,addressId);
    }

    @PreAuthorize("hasAuthority('sys:windowsmanager')")
    @ApiOperation(value = "获取所有窗口号")
    @PostMapping("/getAllWindows/{beSampleUnitsId}")
    public ResResult<?> getAllWindows(@PathVariable Integer beSampleUnitsId,@RequestBody Page page){
        Map<String, Object> pageMap = PageUtil.getPageMap(page);
        pageMap.put("unitsId",beSampleUnitsId);
        List<TblActivitiesAddress> tblActivitiesAddresses = tblActivitiesAddressMapper.getAllWindows(pageMap);
        Integer total = tblActivitiesAddressMapper.getAllWindowsTotal(pageMap);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("tblActivitiesAddresses",tblActivitiesAddresses);
        return new ResResult<>(200,map);
    }

    @PreAuthorize("hasAuthority('sys:windowsmanager')")
    @ApiOperation(value = "由活动id和采样地点id获取窗口号(分页)")
    @PostMapping("/getAllWindowsByIds/{activitiesId}/{addressId}")
    public ResResult<?> getAllWindowsByIds(@PathVariable Integer activitiesId,
                                                    @PathVariable Integer addressId,
                                                    @RequestBody Page page){
        Map<String, Object> pageMap = PageUtil.getPageMap(page);
        pageMap.put("activitiesId",activitiesId);
        pageMap.put("addressId",addressId);
        List<TblActivitiesAddress> tblActivitiesAddresses = tblActivitiesAddressMapper.getAllWindowsByIds(pageMap);
        Integer total = tblActivitiesAddressMapper.getAllWindowsByIdsTotal(pageMap);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("tblActivitiesAddresses",tblActivitiesAddresses);
        return new ResResult<>(200,map);
    }

    @PreAuthorize("hasAuthority('sys:windowsmanager')")
    @ApiOperation(value = "删除窗口号")
    @DeleteMapping("/{activitiesAddressId}")
    public ResResult<?> deleteActivitiesAddressId(@PathVariable Integer activitiesAddressId){
        if ((tblActivitiesAddressMapper.deleteById(activitiesAddressId)) < 1){
            return new ResResult<>(500,"删除失败");
        }
        return new ResResult<>(200,"删除成功");
    }

    @PreAuthorize("hasAuthority('sys:windowsmanager')")
    @ApiOperation(value = "批量删除窗口号")
    @DeleteMapping("/deleteWindowsByIds/{activitiesAddressIds}")
    public ResResult<?> deleteWindowsByIds(@PathVariable Integer[] activitiesAddressIds){
        Integer deleteTotal = tblActivitiesAddressMapper.deleteWindowsByIds(activitiesAddressIds);
        if (deleteTotal < 1){
            return new ResResult<>(500,"删除失败");
        }
        return new ResResult<>(200,"本次删除" + deleteTotal + "条记录");
    }

}

